package com.gdougms.interceptor;

import com.gdougms.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
@Slf4j
public class AdminOperationInterceptor implements HandlerInterceptor {
    @Autowired
    HttpSession httpSession;


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        User user = (User) httpSession.getAttribute("User");
        log.info("进入拦截器UserInterceptor"+user);
        //未登录
        if(user == null){
            log.info("退出管理员拦截器Interceptor-----未登录");
            return false;
        }
        log.info("进入管理员拦截器Interceptor-----已经登录");
        //登录的人没角色
        if(user.getRole() == null){
            return false;
        }
        log.info("进入管理员拦截器Interceptor-----有角色");
        if(user.getRole() != 3 || user.getRole() != 4){
            return true;
        }
        log.info("进入管理员拦截器Interceptor-----通不过管理员拦截器");
        return false;
    }
}
